package top.ucat.boots.starter.oauth2.client.realm;

import org.apache.commons.lang3.StringUtils;
import top.ucat.boots.starter.oauth2.client.beans.oauth.SystemUser;
import top.ucat.boots.starter.oauth2.client.definition.UserCodeTypeDefinition;
import top.ucat.boots.starter.oauth2.client.entity.OauthUserCredentials;

/**
 * @Auther: Jun
 * @Date: 2019/9/4 12:12
 * @Description:
 */
public interface AuthorizingRealm {


    /**
     * 获取扫码登录的用户
     *
     * @param credential
     * @return
     */
    abstract SystemUser getQRUser(OauthUserCredentials credential);

    abstract SystemUser getUserCodeUser(OauthUserCredentials credential);

    abstract SystemUser getPhoneVerificationCodeUser(OauthUserCredentials credential);

    default SystemUser getSystemUser(OauthUserCredentials credential) {
        String userCodeType = credential.getUserCodeType();
        if (StringUtils.isNotBlank(userCodeType)) {
            if (userCodeType.equals(UserCodeTypeDefinition.userCode)) {
                return this.getUserCodeUser(credential);
            } else if (userCodeType.equals(UserCodeTypeDefinition.QR)) {
                return this.getQRUser(credential);
            } else if (userCodeType.equals(UserCodeTypeDefinition.phoneVerificationCode)) {
                return this.getPhoneVerificationCodeUser(credential);
            }
        }
        return null;
    }
}
